package com.wdc.n1;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;

@Slf4j(topic = "c.TestThreadSafe")
public class TestThreadSafe {
    static final int THREAD_NUMBER = 2;
    static final int LOOP_NUMBER = 200;

    public static void main(String[] args) {
        ThreadUnSafe test = new ThreadUnSafe();
        for (int i = 0; i < THREAD_NUMBER; i++) {
            new Thread(() -> {
                test.method1(LOOP_NUMBER);
            },"thread"+(i+1)).start();
        }

    }


}

class ThreadUnSafe {
    ArrayList<String> list = new ArrayList<String>();

    public void method1(int loopNumber) {
        for (int i = 0; i < loopNumber; i++) {
            method2();
            method3();

        }
    }

    public void method2() {
        list.add("1");

    }

    public void method3() {
        list.remove(0);
    }


}
